$(document).ready(function(){
	$("#send_code").click(function(){
		var phone=$("#bind_mobile #phone").val();
		if(!phone_check(phone)){
			alert("手机号码输入有误");
			return false;
		}
		$.get("index.php?c=index&a=send_code&phone="+phone+"&encrypt_code="+$("#encrypt_code").val(),function(rs){
			rs=eval("("+rs+")");
			if(rs.recode==0){
				return false;
			}else{
				retry_send();
				return false;
			}

		});
	});

	$("#submit_bind").click(function(){
		var phone=$("#bind_mobile #phone").val();
		if(!phone_check(phone)){
			alert("手机号码输入有误");
			return false;
		}
		var code=$("#phone_code").val();
		if(!code){alert("请输入手机验证码");return false;}
		$.get("index.php?c=index&a=bind_mobile&code="+code+"&encrypt_code="+$("#encrypt_code").val(),function(rs){
			rs=eval("("+rs+")");
			if(rs.recode==0){
				switch(rs.rs){
					case "same_phone":
						break;
				}
				return false;
			}else{
				window.location.href="index.php?c=index&a=bindmobile_page";
				return false;
			}

		});
	});
});


function phone_check(phone){
	var pattern=/^1\d{10}$/
	return pattern.test(phone);
}


function retry_send(){
	$("#send_code").attr("disabled",true).data("lefts",60);
	var inter=setInterval(function(){
		var second=parseInt($("#send_code").data("lefts"))-1;
		$("#send_code").data("lefts",second);
		if(second>0){
			$("#send_code").val(second+"秒重发");
		}
		else{
			clearInterval(inter);
			$("#send_code").val("重新发送").attr("disabled",false);
		}
	},1000);
}